<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检查状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择检查状态" clearable>
          <el-option
            v-for="dict in dict.type.result_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检查编号" prop="cid">
        <el-input
          v-model="queryParams.cid"
          placeholder="请输入检查编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户姓名" prop="registerId">
        <el-select v-model="queryParams.registerId" filterable placeholder="请选择">
          <el-option
            v-for="item in yuyueList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="问诊手机" prop="registerId">
        <el-select v-model="queryParams.registerId" filterable placeholder="请选择">
          <el-option
            v-for="item in yuyueList"
            :key="item.id"
            :label="item.phone"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检查科室" prop="departmentId">
        <el-select v-model="queryParams.departmentId" filterable placeholder="请选择">
          <el-option
            v-for="item in departmentList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检查大夫" prop="dockerId">
        <el-select v-model="queryParams.dockerId" filterable placeholder="请选择">
          <el-option
            v-for="item in doctorList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="是否作废" prop="isCancel">-->
<!--        <el-select v-model="queryParams.isCancel" placeholder="请选择是否作废" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.sys_yes_no"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="检查时间">
        <el-date-picker
          v-model="daterangeTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['result:result:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['result:result:remove']"-->
<!--        >删除-->
<!--        </el-button>-->
<!--      </el-col>-->

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resultList" @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="id"/>
      <el-table-column label="检查状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.result_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="检查编号" align="center" prop="cid"/>
      <el-table-column label="患者姓名" align="center" prop="register.name"/>
      <el-table-column label="患者性别" align="center" prop="register.sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.register.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="患者年龄" align="center" prop="register.age"/>
      <el-table-column label="手机号码" align="center" prop="register.phone"/>
      <el-table-column label="检查科室" align="center" prop="department.name"/>
      <el-table-column label="检查大夫" align="center" prop="docker.name"/>
      <el-table-column label="检查项目" align="center" prop="project.name"/>
      <el-table-column label="检查费用" align="center" prop="project.money"/>
      <el-table-column label="检查时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="160px">
        <template slot-scope="scope">
          <template>
            <el-button
              size="mini"
              type="primary"
              plain
              @click="handleUpdate(scope.row)"
              v-hasPermi="['result:result:edit']"
            >核实信息</el-button>
            <el-button
              @click="tt(scope.row)"
              :type="scope.row.status === 0? 'success' : 'warning'"
              plain
              size="mini"
              style="margin-left: 16px;"
            >
              {{ scope.row.status === 0? '上传结果' : '开始检查' }}
            </el-button>
          </template>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改结果列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="检查状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择检查状态">
            <el-option
              v-for="dict in dict.type.result_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查编号" prop="cid">
          <el-input v-model="form.cid" placeholder="请输入检查编号"/>
        </el-form-item>
        <el-form-item label="问诊人信息" prop="registerId">
<!--          <el-input v-model="form.registerId" placeholder="请输入问诊人信息"/>-->
          <el-select v-model="form.registerId" filterable placeholder="请选择">
            <el-option
              v-for="item in yuyueList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查科室" prop="departmentId">
<!--          <el-input v-model="form.departmentId" placeholder="请输入检查科室"/>-->
          <el-select v-model="form.departmentId" filterable placeholder="请选择">
            <el-option
              v-for="item in departmentList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查大夫" prop="dockerId">
<!--          <el-input v-model="form.dockerId" placeholder="请输入检查大夫"/>-->
          <el-select v-model="form.dockerId" filterable placeholder="请选择">
            <el-option
              v-for="item in doctorList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查项目" prop="checkId">
<!--          <el-input v-model="form.checkId" placeholder="请输入检查项目"/>-->
          <el-select v-model="form.checkId" filterable placeholder="请选择">
            <el-option
              v-for="item in projectList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="检查费用" prop="money">-->
<!--          <el-input v-model="form.project.money" placeholder="请输入检查费用"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="检查结果图片" prop="img">-->
<!--          <image-upload v-model="form.img"/>-->
<!--        </el-form-item>-->
        <el-form-item label="检查时间" prop="time">
          <el-date-picker clearable
                          v-model="form.time"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择检查时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listResult, getResult, delResult, addResult, updateResult} from "@/api/result/result";
import {listYuyue} from "@/api/register/yuyue";
import { listDepartment} from "@/api/department/department";
import { listDoctor } from "@/api/doctor/doctor";
import { listProject, getProject, delProject, addProject, updateProject } from "@/api/treatment/project";
export default {
  name: "Result",
  dicts: ['result_status','sys_user_sex','sys_yes_no'],
  data() {
    return {
      projectList: [],
      doctorList: [],
      departmentList: [],
      yuyueList: [],  //预约人员表
      // 是否显示弹出层,检查
      drawer: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结果列表表格数据
      resultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 检查时间时间范围
      daterangeTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        cid: null,
        registerId: null,
        departmentId: null,
        dockerId: null,
        checkId: null,
        time: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // result:null

    };
  },
  // beforeRouteLeave(to, from, next) {
  //   // 在离开当前页面时，通过路由参数传递一个标识给原页面
  //   to.query.shouldRefresh = true;
  //   next();
  // },
  created() {
    this.getList();
    this.getRegisterList();
    this.getDepartmentList();
    this.getDoctorList();
    this.getProjectList();
  },
  methods: {
    tt(row){
      // console.log(row);
      this.$router.push({ path: '/result/result/check',query: { patientInfo: row} });
      this.getList();
    },
    /** 查询检查项目 */
    getProjectList() {
      listProject().then(response => {
        this.projectList = response.rows;
      });
    },
    /** 查询医生列表列表 */
    getDoctorList() {
      listDoctor().then(response => {
        this.doctorList = response.rows;
      });
    },
    /** 查询科室管理列表 */
    getDepartmentList() {
      listDepartment().then(response => {
        this.departmentList = response.rows;
      });
    },
    /** 查询预约人员信息 */
    getRegisterList() {
      listYuyue().then(response => {
        this.yuyueList = response.rows;
      });
    },
    /** 查询结果列表列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeTime && '' != this.daterangeTime) {
        this.queryParams.params["beginTime"] = this.daterangeTime[0];
        this.queryParams.params["endTime"] = this.daterangeTime[1];
      }
      listResult(this.queryParams).then(response => {
        this.resultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        cid: null,
        registerId: null,
        departmentId: null,
        dockerId: null,
        checkId: null,
        money: null,
        img: null,
        time: null,
        del: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加结果列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.reset();
      const id = row.id || this.ids
      getResult(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改结果列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResult(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResult(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除结果列表编号为"' + ids + '"的数据项？').then(function () {
        return delResult(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('result/result/export', {
        ...this.queryParams
      }, `result_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
